package com.partvoyage.client.mvp2.view.actionPoint;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;
import com.partvoyage.client.mvp2.event.LoadDataDetailPointEvent;
import com.partvoyage.client.mvp2.event.LoadDataDetailPointEvent.TYPE_DETAIL_POINT;
import com.partvoyage.client.mvp2.framework.MVPDisplay;
import com.partvoyage.client.mvp2.framework.MVPPresenter;
import com.partvoyage.client.mvp2.view.carte.service.CartePresentationService;
import com.partvoyage.shared.CodePays;
import com.partvoyage.shared.InfoPointBean;
import com.partvoyage.shared.InfoPointBean.TYPE_ZOOM_LEVEL;
import com.smartgwt.client.widgets.layout.HLayout;

public class DetailsPointPresenter extends
		MVPPresenter<DetailsPointPresenter.Display> {

	public interface Display extends MVPDisplay {
		void miseAJourOu(String ou);

		void miseAJourDrapeau(String url);

		void afficherCacherDrapeau(Boolean afficherCacher);
	}

	public DetailsPointPresenter(final Display display,
			final HandlerManager eventBus) {
		super(display, eventBus);
		bind();
	}

	/**
	 * Lie le bouton send à l'action ....
	 */
	protected void bind() {

	}

	@Override
	public void go(final HasWidgets container) {

	}

	public void go(HLayout container) {
		// container.clear();
		container.addChild(display.asWidget());

	}

	@Override
	protected void registerHandler() {
		eventBus.addHandler(LoadDataDetailPointEvent.TYPE,
				new LoadDataDetailPointEvent.LoadDataDetailPointEventHandler() {

					@Override
					public void onLoadDataDetailPoint(
							final LoadDataDetailPointEvent event) {
						if (event.getTypeInformation().equals(
								TYPE_DETAIL_POINT.TYPE_NETTOYAGE)) {
							display.miseAJourOu("");
							display.afficherCacherDrapeau(false);
						} else if (event.getTypeInformation().equals(
								TYPE_DETAIL_POINT.TYPE_REALOAD_OU)) {
							InfoPointBean infoPointBean = event
							.getInfoPointBean();
							miseAJourOu(infoPointBean);
						}
					}

					private void miseAJourOu(InfoPointBean infoPointBean) {
						String ou = infoPointBean.getOu();
						if (ou != null) {
							TYPE_ZOOM_LEVEL zoomLevel = CartePresentationService
									.getZoomLevel(infoPointBean
											.getZoomLevel());
							if (zoomLevel
									.equals(TYPE_ZOOM_LEVEL.PAYS_LEVEL)) {
								ou = CodePays.getNomPays(ou);
							}
							display.miseAJourOu(ou);
						} else {
							display.miseAJourOu("");
						}
						String codePays = infoPointBean.getCodePays();
						if (codePays != null) {
							String url = "http://www.geonames.org/flags/x/"
									+ codePays.toLowerCase() + ".gif";
							display.afficherCacherDrapeau(true);
							display.miseAJourDrapeau(url);
						} else {
							display.afficherCacherDrapeau(false);
						}
					}

				});
	}
}
